<?php 

require_once("air_model.php");
class Virair_model extends Air_model{
	
	private $wsdl="http://ws.jzinfo.sh.cn/vibe2/vflightservice.asmx?wsdl";
	
	function Virair_model()
	{
	    parent:: __construct();
	}
	
	//航班查询并返回对应的航班数据
	//$isPFD表示是否使用私有运价
	public function avinternationalflight($sFromport,$sDestport,$dFlightdate,$argoData)
	{
		$sAirline		=$argoData["sAirline"];
		$isback		=$argoData["isback"];
		$FlightType    =$argoData["FlightType"];
		$isPFD	     =$argoData["isPFD"];
		$goClass		=$argoData["goClass"];
		$goMix		=$argoData["goMix"];
		
		$ArrayPolicy=array();
		$arFlight=array();
		
		//国际航班使用国际运价换算费率
		$this->Currencyrate=$this->internationrate;
		
		//获取AV数据
		$lstFlight=$this->ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline);
		$iFlight=count($lstFlight);
		
		//获取FD数据,私有运价不需要获取FD数据
		$ArrayFD=array("TPM"=>0,"ClassYPrice" =>0);
		
		//获取税费数据，
		$ArrayTax=$this->getTaxbydb($sFromport,$sDestport,$dFlightdate);
		
		
		//获取私有运价数据
		$ArrayPFD=$this->getPrivatefreight($sFromport,$sDestport,$dFlightdate,$FlightType,$isback,"I");
		
			
		
		//输出航班详细信息
		for($i=0;$i<$iFlight;$i++)
		{
			$item=explode("|",$lstFlight[$i]);
			//FM9370|20110510|0655|0920|true|738|0|F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,|SZX|SHA
			
			
			if($sAirline==substr($item[0],0,2) ||$sAirline=="")
			{
				//回程航班查询的话必须符合去程信息				
				if(!$isback || ($isback && $item[8]==$sFromport && $item[9]==$sDestport))
				{
					
					$flightdata=array("ClassList" => $item[7],
								"flightnum"=> $item[0],
								"FlightType"=> $FlightType,
								"FlightDate" => $dFlightdate,
								"isback" => $isback,
								"goClass"	=> $goClass,
								"goMix"	=> $goMix
							);
							
					$ClassData=$this->getClassPrice($ArrayFD,$ArrayPFD,$ArrayPolicy,$flightdata);
					$iOilfee=$this->getInternationalTax($ArrayTax,substr($item[0],0,2))*$this->Currencyrate;

					//计算运价
					$arFlight[$i]=array("FlightNo" => $item[0],
									"Airline" => substr($item[0],0,2),
									"flightdate" => $item[1],
									"Depart" => implode(str_split($item[2],2),":"),
									"Arrival" => implode(str_split($item[3],2),":"),
									"Plane" => $item[5],
									"Stop" => $item[6],
									"class" => $ClassData,
									"fromport" => $item[8],
									"destport" => $item[9],
									"fromportName" => $this->getAirportname_buffer($item[8]),
									"destportName" => $this->getAirportname_buffer($item[9]),
									"OilFee" => ceil($iOilfee),
									"AirportFee" => ceil($this->iAirportFee),
									"mainCurrency" => $this->mainCurrency
								);
								
				}
			}
			
		}
		
		return $arFlight;
		
	}
	
	
	//航班查询
	public function ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline)
	{
		
		$client = new SoapClient($this->wsdl);
		$param = array('Fromport'=> $sFromport,
				'Destport'=> $sDestport,
				'FlightDate'=> $dFlightdate,
				"Hashcode"=> $this->sHashCode,
				"Airline"=> ""
				);
		
		$out = $client->AVVFlight($param); 
		$arResult = $out->AVVFlightResult->ReturnValue;
		
		$lstFlight=explode("-",$arResult);
		
		
		return $lstFlight;

	}
	
	
	//产生PNR
	public function  ws_CreatePNR($Tel,$arFlightInfo,$arPassenger,$RMK)
	{
		//return explode(",","0000,XXXXX,HKG261");
		
		
		//SZX,SHA^SHA,SZX^ZH9815,ZH9814^Y,V^2010-11-13,2010-11-17^17:10
		
		$flightxml="";
		$icount=count($arFlightInfo);
		for($i=0;$i< $icount;$i++)
		{
			$arFlightInfo[$i]["departtime"]=str_replace(":","",$arFlightInfo[$i]["departtime"]);
			
			$flightxml.='<flight>'.
				'<where fromport="'.$arFlightInfo[$i]["fromport"].'" destport="'.$arFlightInfo[$i]["destport"].'" ClassCode="'.$arFlightInfo[$i]["classcode"].'"  />'.
				'<when flightdate="'.$arFlightInfo[$i]["flightdate"].'" departtime="'.$arFlightInfo[$i]["departtime"].'"  Carrier="'.substr($arFlightInfo[$i]["flightno"],0,2).'" flightno="'.$arFlightInfo[$i]["flightno"].'" />'.
				'</flight>';
		}
		
		$passengerxml="";
		foreach($arPassenger as $item)
		{
			$passengerxml.='<Passenger Name="'.$item["Name"].'" IDType="NI"'.
				 ' CardNo="'.$item["CardNo"].'" Country="'.$item["Nationality"].'"'.
				 ' BirthDay="'.$item["PI_userBirth"].'" Gender="'.$item["PI_userGender"].'" idExpiration="'.$item["PI_idExpiration"].'"  '.
				 ' MobilePhone="'.$item["PassengerTel"].'" />';
		}
		
		
		
		$pnrxml='<?xml version="1.0" encoding="utf-8"?>'.
				'<PNRDATA><cmd>Create</cmd>'.
				'<TicketLimitDate></TicketLimitDate>'.
				'<TicketLimitTime></TicketLimitTime>'.
				'<Telephone>'.$Tel.'</Telephone>'.
				'<RMKS>'.$RMK.'</RMKS>'.
				'<vflightGroup>'.$flightxml.				
				'</vflightGroup>'.
				'<vPassenger>'.
				$passengerxml.
				'</vPassenger>'.
				'<OtherSegments><OtherSegment></OtherSegment></OtherSegments></PNRDATA>';
		//echo $pnrxml;
		
		//exit();
		
		$client = new SoapClient($this->wsdl);  
		$param = array('vPNRXML'=> $pnrxml,
				"Hashcode"=> $this->sHashCode
				);
		$out = $client->vPNRcmd($param); 
		$pnrcontext = $out->vPNRcmdResult;
		
	
		return array($pnrcontext->ICode,$pnrcontext->ReturnValue,"");
		
	}
	
}


?>